import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    collapse: true,
    tagsList: []
  },
  mutations: {
    delTagsItem (state, data) {
      state
        .tagsList
        .splice(data.index, 1)
    },
    setTagsItem (state, data) {
      state
        .tagsList
        .push(data)
    },
    clearTags (state) {
      const welcome = []
      state.tagsList.forEach((item) => {
        if (item.name === 'welcome') {
          welcome.push(item)
        }
      })
      state.tagsList = []
      state.tagsList = welcome
    },
    closeTagsOther (state, data) {
      const obj = {}
      data = data.reduce((curs, next) => {
        (obj[next.name] ? '' : obj[next.name] = true) && curs.push(next)
        return curs
      }, [])
      state.tagsList = data
    },
    closeCurrentTag (state, data) {
      for (let i = 0, len = state.tagsList.length; i < len; i++) {
        const item = state.tagsList[i]
        if (item.path === data.$route.fullPath) {
          if (i < len - 1) {
            data
              .$router
              .push(state.tagsList[i + 1].path)
          } else if (i > 0) {
            data
              .$router
              .push(state.tagsList[i - 1].path)
          } else {
            data
              .$router
              .push('/welcome')
          }
          state
            .tagsList
            .splice(i, 1)
          break
        }
      }
    }
  },
  actions: {},
  modules: {}
})
